package main

import (
	"fmt"
	"sync"
)

/**
	wait group
 */


func doWorker(id int, c chan int, w worker)  {
	for {
		for n := range c {
			fmt.Printf("worker %d received %c \n", id, n)
			//go func() {
			//	done <- true
			//}()
			w.done()
		}

	}
}
func doWorker1(id int, c chan int, done chan bool)  {
	for {
		for n := range c {
			fmt.Printf("worker %d received %c \n", id, n)
			//go func() {
			//	done <- true
			//}()
			done <- true

		}

	}
}

func createWorker(id int, wg *sync.WaitGroup) worker {
	w := worker{
		in : make(chan int),
		//done: make(chan bool),
		//wg: wg,
		done: func() {
			wg.Done()
		},
	}
	//go doWorker(id, w.in, w.done)
	go doWorker(id, w.in, w)
	return w
}

type worker struct {
	in chan int
	//done chan bool
	//wg *sync.WaitGroup
	done func()
}

func chanDemo()  {
	var wg sync.WaitGroup
	wg.Add(20)
	var workers [10]worker  //数组
	for i := 0; i < 10; i++ {
		workers[i] = createWorker(i, &wg)
	}
	for i, worker := range workers{
		worker.in <- 'a' + i
	}
	//for _, worker := range workers {
	//	<- worker.done
	//}
	for i, worker := range workers{
		worker.in <- 'A' + i
	}
	//for _, worker := range workers {
	//	<- worker.done
	//}
	//for _, worker := range workers {
	//	<- worker.done
	//	<- worker.done
	//}
	wg.Wait()
}

func main() {
	chanDemo()

}
